package com.senlingspace.web.controller.headquarters;

import com.senlingspace.common.annotation.Log;
import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.core.page.TableDataInfo;
import com.senlingspace.common.enums.BusinessType;
import com.senlingspace.system.domain.SlkjProduct;
import com.senlingspace.system.domain.dto.ShopProductDto;
import com.senlingspace.system.domain.vo.SlkjProductVO;
import com.senlingspace.system.service.ISlkjProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理Controller
 *
 * @author wangliang
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/system/product")
public class SlkjProductController extends BaseController {
    @Autowired
    private ISlkjProductService slkjProductService;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlkjProduct slkjProduct, Long shopId) {
        List<SlkjProductVO> list = slkjProductService.selectSlkjProductList(slkjProduct, shopId);
        return getDataTable(list);
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:productShop:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        SlkjProductVO slkjProductVO = slkjProductService.selectSlkjProductByProductId(productId);
        return success(slkjProductVO);
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlkjProductVO slkjProduct) {
        try {
            return toAjax(slkjProductService.insertSlkjProduct(slkjProduct));
        }catch (IllegalArgumentException e){
            return new AjaxResult(500,e.getMessage());
        }
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlkjProductVO slkjProduct) {
        try {
            return toAjax(slkjProductService.updateSlkjProduct(slkjProduct));
        }catch (IllegalArgumentException e){
            return new AjaxResult(500,e.getMessage());
        }

    }


    /**
     * 商品上下架pos端上下架
     */
    @PreAuthorize("@ss.hasPermi('system:productShop:updateStatus')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateShopProductStatus(@RequestBody ShopProductDto shopProductDto) {
        return toAjax(slkjProductService.updateShopProductStatus(shopProductDto));
    }

    /**
     * 商品上下架小程序端上下架
     */
    @PreAuthorize("@ss.hasPermi('system:updateAppStatus')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update/app/status")
    public AjaxResult updateProductAppStatus(@RequestBody ShopProductDto shopProductDto) {
        return toAjax(slkjProductService.updateAppStatus(shopProductDto.getStatus(),shopProductDto.getProductId()));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long productIds) {
        return toAjax(slkjProductService.deleteSlkjProductByProductId(productIds));
    }



}
